package com.briup.jd2407cms.web.controller;

import com.briup.jd2407cms.aop.Logging;
import com.briup.jd2407cms.domain.Slideshow;
import com.briup.jd2407cms.service.SlideshowService;
import com.briup.jd2407cms.util.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: JD2407-cms
 * @BelongsPackage: com.briup.jd2407cms.web.controller
 * @Author: 鲨鲨天下第一
 * @CreateTime: 2024-11-18  14:45
 * @Description: TODO
 * @Version: 1.0
 */
@Api(tags = "轮播图模块")
@RestController
@RequestMapping("/slideshow")
public class SlideshowController {

    @Autowired
    private SlideshowService slideshowService;

    @ApiOperation(value = "根据id查询轮播图信息", notes = "用于更新时的数据回显")
    @Logging("根据id查询轮播图信息")
    @GetMapping("/queryById/{id}")
    public Result findById(@PathVariable Integer id) {
        return Result.success(slideshowService.getById(id));
    }

    @ApiOperation(value = "查询所有启用的轮播图", notes = "按照状态查询")
    @Logging("查询所有启用的轮播图")
    @GetMapping("/queryAllEnable")
    public Result getAllEnable() {
        return Result.success(slideshowService.queryAllEnable());
    }

    @ApiOperation(value = "查询所有轮播图", notes = "分页查询 1、条件查询(按status、description) 2、按时间倒序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "条数",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "desc",value = "轮播图描述",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "status",value = "状态，是否启用",paramType = "query",dataType = "java.lang.String"),
    })
    @Logging("查询所有轮播图")
    @GetMapping("/query")
    public Result getByPageCondition(@RequestParam Integer pageNum,
                                     @RequestParam Integer pageSize,
                                     String status, String description) {
        return Result.success(slideshowService.queryByPageCondition(pageNum, pageSize, status, description));
//        //1 创建IPage分页对象,设置分页参数
//        IPage<SlideshowExtend> page = new Page<>(pageNum, pageSize);
//        //2 执行分页查询
//        LambdaQueryWrapper<SlideshowExtend> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(status != null, SlideshowExtend::getStatus,status)
//                .like(description != null,SlideshowExtend::getDescription,description)
//                .orderByDesc(SlideshowExtend::getUploadTime);
//        //3 获取分页结果
//        return Result.success(slideshowService.page(page, lqw));
    }

    @ApiOperation(value = "单个删除轮播图", notes = "前端会传入id")
    @Logging("单个删除轮播图")
    @DeleteMapping("/deleteById/{id}")
    public Result removeById(@PathVariable String id) {
        return Result.success(slideshowService.removeById(id));
    }

    @ApiOperation(value = "批量删除轮播图", notes = "前端会传入ids")
    @Logging("批量删除轮播图")
    @DeleteMapping("/deleteByBatch/{ids}")
    public Result removeBatch(@PathVariable List<Integer> ids) {
        return Result.success(slideshowService.removeByIds(ids));
    }

    @ApiOperation(value = "新增或修改轮播图", notes = "id为空为新增，且新增轮播图url必须唯一" +
            "；id不为null，启用、禁用状态修改及其他信息修改，如果要修改url，则其必须唯一")
    @Logging("新增或修改轮播图")
    @PostMapping("/saveOrUpdate")
    public Result addOrModify(@RequestBody Slideshow slideshow) {
        boolean b = slideshowService.saveOrUpdate(slideshow);
        return Result.success(b?"操作成功":"操作失敗");
    }


}
